package sybel.pan;

import javax.swing.JPanel;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import sybel.DOMPanelInterface;

public class Happening extends javax.swing.JPanel implements DOMPanelInterface {

    /** Creates new form Happening */
    public Happening(Node node) {
        initComponents();
        if(node.getNodeName() != "happening"){ // caso trigger
            Parent = node;
            happening = (Element)Parent.getFirstChild();
            if(happening != null)
                initPanel();
        }else{ // caso generale
            Parent = node.getParentNode();
            happening = (Element)node;
            initPanel();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jCBActor = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jTextStepName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextStepID = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jCBResultVariable = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextDescription = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        jCBHappening = new javax.swing.JComboBox();

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 26, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 19, Short.MAX_VALUE)
        );

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel4.setText("Actor:");

        jCBActor.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Default" }));
        jCBActor.setMaximumSize(new java.awt.Dimension(50, 50));

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("StepName:");

        jTextStepName.setMaximumSize(new java.awt.Dimension(50, 50));

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("StepID:");

        jTextStepID.setMaximumSize(new java.awt.Dimension(50, 50));

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel5.setText("Result Variable:");

        jCBResultVariable.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jCBResultVariable.setMaximumSize(new java.awt.Dimension(50, 50));

        jTextDescription.setColumns(20);
        jTextDescription.setRows(5);
        jTextDescription.setPreferredSize(new java.awt.Dimension(164, 70));
        jScrollPane1.setViewportView(jTextDescription);

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setText("Description:");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE))
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jCBResultVariable, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jTextStepID, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jTextStepName, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jCBActor, 0, 162, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 284, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jCBActor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextStepName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextStepID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jCBResultVariable, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        jCBHappening.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Choose one...", "action execute", "property Get", "property Set" }));
        jCBHappening.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jCBHappeningItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(197, 197, 197))
            .addGroup(layout.createSequentialGroup()
                .addComponent(jCBHappening, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jCBHappening, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel3Layout.setHonorsVisibility(false);
        jPanel3.setVisible(false);
    }// </editor-fold>//GEN-END:initComponents

/*  Alla modifica della selezione nella combobox questo event listener instanzia
 *  e disegna il rispettivo pannello in output
 */
    private void jCBHappeningItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCBHappeningItemStateChanged
        HappeningSelector();
    }//GEN-LAST:event_jCBHappeningItemStateChanged

    public void initPanel(){
        setStepName(happening.getAttribute("stepName"));
        setStepID(happening.getAttribute("stepID"));
        if(happening.getElementsByTagName("description").item(0) != null)
            setDescription(happening.getElementsByTagName("description").item(0).getTextContent());
        setActor();

        NodeList nl = happening.getElementsByTagName("actionExecute");
        if(nl.item(0) != null)
            setHappeningBody(1);

        nl = happening.getElementsByTagName("propertyGet");
        if(nl.item(0) != null)
            setHappeningBody(2);

        nl = happening.getElementsByTagName("propertySet");
        if(nl.item(0) != null)
            setHappeningBody(3);

        HappeningSelector();
    }

    public String getHappeningBody(){
        int temp = jCBHappening.getSelectedIndex();
        switch(temp){
            case 1:
                return "actionExecute";
            case 2:
                return "propertyGet";
            case 3:
                return "propertySet";
            default:
                return null;
        }
    }
    
    public String getStepName(){
        return jTextStepName.getText();
    }
    
    public String getStepID(){
        return jTextStepID.getText();
    }
    
    public String getDescription(){
        return jTextDescription.getText();
    }

    public String getActor(){
        return (String)jCBActor.getSelectedItem();
    }

    public String getResultVariable(){
        return (String)jCBResultVariable.getSelectedItem();
    }

    public void setStepName(String s){
        jTextStepName.setText(s);
    }

    public void setStepID(String s){
        jTextStepID.setText(s);
    }

    public void setDescription(String s){
        jTextDescription.setText(s);
    }
    
    /**
     * Metodo per preimpostare il contenuto dell'Happening attraverso il passaggio di
     * un intero identificante il corpo dell'happening tra actionexecute,propertyGet e propertySet
     * @param h passare: 1 per actionExecute, 2 propertyGet, 3 propertySet
     *
     */
    public void setHappeningBody(int h){
        switch(h){
            case 1:// action Execute
                jCBHappening.setSelectedIndex(h);
            case 2:// propertyGet
                jCBHappening.setSelectedIndex(h);
            case 3:// propertySet
                jCBHappening.setSelectedIndex(h);
            default:
                jCBHappening.setSelectedIndex(0);
        }
    }

 private void HappeningSelector(){
        switch(jCBHappening.getSelectedIndex()){

            // Action Execute
            case 1:
                ActionExecute AEPanel;
                if(happening == null){
                    AEPanel = new ActionExecute(Parent);
                }else{
                    AEPanel = new ActionExecute(happening);
                }
                setEntityPanel(AEPanel);
                break;

            // Property Get
            case 2:
                PropertyGet propertyGet;
                if(happening == null){
                    propertyGet = new PropertyGet(Parent);
                }else{
                    propertyGet = new PropertyGet(happening);
                }
                setEntityPanel(propertyGet);
                break;

            // Property Get
            case 3:
                PropertySet propertySet;
                if(happening == null){
                    propertySet = new PropertySet(Parent);
                }else{
                    propertySet = new PropertySet(happening);
                }
                setEntityPanel(propertySet);
                break;
            default:
                jPanel3.removeAll();
                jPanel3.repaint();
        }
    }

 private void setEntityPanel(JPanel entity){
        /* Imposto il layout inserendo il pannello Action Execute nel pannello
         * adibito ad ospitare i componenti scelti nella combobox   */
        jPanel3.removeAll();
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(entity, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(entity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                );

        // rendo visibile il nuovo pannello
        jPanel3.repaint();
        jPanel3.setVisible(true);
    }


    public void setActor(){}
    public void setResultVariable(){}

    public Node saveData(){
        Document doc = Parent.getOwnerDocument();
        happening = doc.createElement("happening");
        if(!getStepName().isEmpty())
            happening.setAttribute("stepName",getStepName());
        if(!getStepID().isEmpty())
            happening.setAttribute("stepID",getStepID());

        if(!getDescription().isEmpty()){
            Element Description = doc.createElement("description");
            Description.setTextContent(getDescription());
            happening.appendChild(Description);
        }

        if(getActor() != "Default"){

        }

        int temp = jCBHappening.getSelectedIndex();
        switch(temp){
            case 1:
                ActionExecute ae = (ActionExecute)jPanel3.getComponents()[0];
                happening.appendChild(ae.saveData());
                break;
            case 2:
                PropertyGet propget = (PropertyGet)jPanel3.getComponents()[0];
                happening.appendChild(propget.saveData());
                break;
            case 3:
                PropertySet propset = (PropertySet)jPanel3.getComponents()[0];
                happening.appendChild(propset.saveData());
                break;
            default:
                break;
        }
        
        return (Node)happening;
    }

    public Node AppendSavedData(){
        Node temp = happening;
        Parent.replaceChild(saveData(), temp);
        return Parent;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox jCBActor;
    private javax.swing.JComboBox jCBHappening;
    private javax.swing.JComboBox jCBResultVariable;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextDescription;
    private javax.swing.JTextField jTextStepID;
    private javax.swing.JTextField jTextStepName;
    // End of variables declaration//GEN-END:variables
    private Node Parent = null;
    private Element happening = null;
}
